package com.linngdu664.bettersnowballfight;

import com.linngdu664.bettersnowballfight.itemclass.SmoothSnowballItem;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.BlazeEntity;
import net.minecraft.item.*;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.util.ActionResult;

public class Main implements ModInitializer {
    @Override
    public void onInitialize() {
        Register.register();

        AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
            Item item = player.getMainHandStack().getItem();
            if (!player.isSpectator() && entity instanceof LivingEntity) {
                if (item instanceof PowderSnowBucketItem) {
                    entity.setFrozenTicks(240);
                    ((LivingEntity)entity).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 100, 1));
                    ((LivingEntity)entity).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20, 2));
                    ((LivingEntity)entity).addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 150, 1));
                    if (!player.getAbilities().creativeMode) {
                        player.getStackInHand(hand).decrement(1);
                        player.getInventory().offer(new ItemStack(Items.BUCKET, 1), true);
                    }
                    for (int i = 0; i < 32; i++) {
                        world.addParticle(ParticleTypes.ITEM_SNOWBALL, entity.getX(), entity.getEyeY(), entity.getZ(), 0, 0, 0);
                    }
                    if(entity instanceof BlazeEntity){
                        entity.damage(DamageSource.player(player), 8);
                    }
                } else if (item instanceof SnowballItem || item instanceof SmoothSnowballItem) {
                    entity.setFrozenTicks(180);
                    ((LivingEntity)entity).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20, 1));
                    ((LivingEntity)entity).addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 30, 1));
                    if (!player.getAbilities().creativeMode) {
                        player.getStackInHand(hand).decrement(1);
                    }
                    for (int i = 0; i < 16; i++) {
                        world.addParticle(ParticleTypes.ITEM_SNOWBALL, entity.getX(), entity.getEyeY(), entity.getZ(), 0, 0, 0);
                    }
                    if (entity instanceof BlazeEntity) {
                        entity.damage(DamageSource.player(player), 4);
                    }
                }
            }
            return ActionResult.PASS;
        });
    }
}
